package com.web_bloks.mvc.client;

import java.util.Enumeration;
import java.util.Vector;

import com.google.gwt.core.client.GWT;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class ChainCommand extends Notifier implements ICommand {

	private Vector<Class<ICommand>> subCommands = null;

	/**
	 * Constructor.
	 * 
	 * <P>
	 * You should not need to define a constructor, instead, override the
	 * <code>initializeMacroCommand</code> method.
	 * </P>
	 * 
	 * <P>
	 * If your subclass does define a constructor, be sure to call
	 * <code>super()</code>.
	 * </P>
	 */
	public ChainCommand( ) {
		this.subCommands = new Vector<Class<ICommand>>();
		initializeMacroCommand();
	}

	/**
	 * Initialize the <code>MacroCommand</code>.
	 */
	protected void initializeMacroCommand( ) {
	}

	/**
	 * Add a <i>SubCommand</i>.
	 * 
	 * <P>
	 * The <i>SubCommands</i> will be called in First In/First Out (FIFO)
	 * order.
	 * </P>
	 * 
	 * @param commandClassRef
	 *            a reference to the <code>Class</code> of the
	 *            <code>ICommand</code>.
	 */
	protected void addSubCommand( Class<ICommand> commandClassRef ) {
		this.subCommands.addElement( commandClassRef );
	}

	/**
	 * Execute this <code>MacroCommand</code>'s <i>SubCommands</i>.
	 * 
	 * <P>
	 * The <i>SubCommands</i> will be called in First In/First Out (FIFO)
	 * order.
	 * 
	 * @param notification
	 *            the <code>INotification</code> object to be passsed to each
	 *            <i>SubCommand</i>.
	 */
	public void execute( INotification notification ) {

		if (null==this.subCommands)
			return;
		
		Class<ICommand> temp = null;
		for (Enumeration<Class<ICommand>> enu = this.subCommands.elements(); enu.hasMoreElements();) {
			temp = (Class<ICommand>) enu.nextElement();
			try {
				//ICommand commandInstance = (ICommand) temp.newInstance();
				ICommand commandInstance = (ICommand) GWT.create( temp.getClass() );
				commandInstance.execute( notification );
			} catch (Exception e) {
			}
		}
	}

	
}//